package com.sc.nft.service.impl;

import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.service.MallScriptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author liGen
 * @version 1.0
 * @date 2024/1/30 6:23 下午
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallScriptServiceImpl implements MallScriptService {
    public final SupplierManageDao supplierManageDao;
    public final SupplierManageBindingDao supplierManageBindingDao;
    public final MallActivityDao mallActivityDao;
    public final MallProductDao mallProductDao;
    public final MallActivitySpecsDao mallActivitySpecsDao;
    public final MallDaoWalletDao mallDaoWalletDao;
    public final UserCreatorInfoDao userCreatorInfoDao;
    public final RankingBoardConfDao rankingBoardConfDao;
    public final MallRankingListPoolDao mallRankingListPoolDao;

    /**
     * 供应商商品dao处理
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disposeDao() {
        // 供应商商品dao处理
        List<SupplierManageBinding> list = supplierManageBindingDao.getList();
        for (SupplierManageBinding supplierManageBinding : list) {
            SupplierManage supplierManage = supplierManageDao.get(supplierManageBinding.getSupplierId());
            if (Objects.nonNull(supplierManage)){
                Long productId = supplierManageBinding.getMallId();
                if (Objects.nonNull(supplierManage.getDaoId()) && 0L != supplierManage.getDaoId()){
                    mallProductDao.updateDao(productId,supplierManage.getDaoId());
                    mallActivityDao.updateDao(productId,supplierManage.getDaoId());
                }
            }
        }
    }

    /**
     * 添加商城Dao钱包
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addMallDaoWallet() {
        List<UserCreatorInfo> list = userCreatorInfoDao.getList();
        for (UserCreatorInfo userCreatorInfo : list) {
            MallDaoWallet mallDaoWallet = mallDaoWalletDao.getByDaoIdNotException(userCreatorInfo.getId());
            if (Objects.isNull(mallDaoWallet)){
                mallDaoWallet = new MallDaoWallet();
                mallDaoWallet.setDaoId(userCreatorInfo.getId());
                mallDaoWallet.setBalance(BigDecimal.ZERO);
                mallDaoWallet.setActivityBalance(BigDecimal.ZERO);
                mallDaoWallet.setFreezeAmount(BigDecimal.ZERO);
                mallDaoWallet.setSumIncome(BigDecimal.ZERO);
                mallDaoWallet.insert();
            }
        }
    }

    /**
     * 分润脚本处理
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProfitTemplate(Long id) {
        // 分润脚本处理
        mallActivitySpecsDao.updateProfitTemplateId(id);
    }

    /**
     * 排行榜金额处理
     *
     * @param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void leaderboardAmountProcessing() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = null;
        Date end = null;
        try {
            start = sdf.parse("2024-01-01 00:00:00");
            end = sdf.parse("2024-01-31 23:59:59");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BigDecimal ipMallTradingProfit = rankingBoardConfDao.getIpMallTradingProfit(start, end);
        MallRankingListPool residualPrizePool = mallRankingListPoolDao.getResidualPrizePool();
        mallRankingListPoolDao.addAmount(ipMallTradingProfit,residualPrizePool.getId());
    }
}
